package com.github.shuaidd.service.mapper;

import com.github.pagehelper.Page;
import com.github.shuaidd.model.repository.RelatedContentEntityMapper;
import org.activiti.app.domain.runtime.RelatedContent;
import org.activiti.engine.impl.cmd.AbstractCustomSqlExecution;
import org.springframework.stereotype.Service;

/**
 * 描述
 *
 * @author ddshuai
 * @date 2020-07-20 21:01
 **/
@Service
public class RelatedContentMapperService extends BaseMapperService implements RelatedContentEntityMapper {

    @Override
    public int deleteByPrimaryKey(Long id) {
        return managementService.executeCustomSql(new AbstractCustomSqlExecution<RelatedContentEntityMapper, Integer>(RelatedContentEntityMapper.class) {
            @Override
            public Integer execute(RelatedContentEntityMapper relatedContentEntityMapper) {
                return relatedContentEntityMapper.deleteByPrimaryKey(id);
            }
        });
    }

    @Override
    public int insert(RelatedContent record) {
        record.setId(System.currentTimeMillis());
        return managementService.executeCustomSql(new AbstractCustomSqlExecution<RelatedContentEntityMapper, Integer>(RelatedContentEntityMapper.class) {
            @Override
            public Integer execute(RelatedContentEntityMapper relatedContentEntityMapper) {
                return relatedContentEntityMapper.insert(record);
            }
        });
    }

    @Override
    public int insertSelective(RelatedContent record) {
        return managementService.executeCustomSql(new AbstractCustomSqlExecution<RelatedContentEntityMapper, Integer>(RelatedContentEntityMapper.class) {
            @Override
            public Integer execute(RelatedContentEntityMapper relatedContentEntityMapper) {
                return relatedContentEntityMapper.insertSelective(record);
            }
        });
    }

    @Override
    public RelatedContent selectByPrimaryKey(Long id) {
        return managementService.executeCustomSql(new AbstractCustomSqlExecution<RelatedContentEntityMapper, RelatedContent>(RelatedContentEntityMapper.class) {
            @Override
            public RelatedContent execute(RelatedContentEntityMapper relatedContentEntityMapper) {
                return relatedContentEntityMapper.selectByPrimaryKey(id);
            }
        });
    }

    @Override
    public int updateByPrimaryKeySelective(RelatedContent record) {
        return managementService.executeCustomSql(new AbstractCustomSqlExecution<RelatedContentEntityMapper, Integer>(RelatedContentEntityMapper.class) {
            @Override
            public Integer execute(RelatedContentEntityMapper relatedContentEntityMapper) {
                return relatedContentEntityMapper.updateByPrimaryKeySelective(record);
            }
        });
    }

    @Override
    public int updateByPrimaryKey(RelatedContent record) {
        return managementService.executeCustomSql(new AbstractCustomSqlExecution<RelatedContentEntityMapper, Integer>(RelatedContentEntityMapper.class) {
            @Override
            public Integer execute(RelatedContentEntityMapper relatedContentEntityMapper) {
                return relatedContentEntityMapper.updateByPrimaryKey(record);
            }
        });
    }

    @Override
    public Page<RelatedContent> findAllRelatedBySourceAndSourceId(String source, String sourceId) {
        return managementService.executeCustomSql(new AbstractCustomSqlExecution<RelatedContentEntityMapper, Page<RelatedContent>>(RelatedContentEntityMapper.class) {
            @Override
            public Page<RelatedContent> execute(RelatedContentEntityMapper relatedContentEntityMapper) {
                return relatedContentEntityMapper.findAllRelatedBySourceAndSourceId(source, sourceId);
            }
        });
    }

    @Override
    public Page<RelatedContent> findAllRelatedByTaskId(String taskId) {
        return managementService.executeCustomSql(new AbstractCustomSqlExecution<RelatedContentEntityMapper, Page<RelatedContent>>(RelatedContentEntityMapper.class) {
            @Override
            public Page<RelatedContent> execute(RelatedContentEntityMapper relatedContentEntityMapper) {
                return relatedContentEntityMapper.findAllRelatedByTaskId(taskId);
            }
        });
    }

    @Override
    public Page<RelatedContent> findAllFieldBasedContentByTaskId(String taskId) {
        return managementService.executeCustomSql(new AbstractCustomSqlExecution<RelatedContentEntityMapper, Page<RelatedContent>>(RelatedContentEntityMapper.class) {
            @Override
            public Page<RelatedContent> execute(RelatedContentEntityMapper relatedContentEntityMapper) {
                return relatedContentEntityMapper.findAllFieldBasedContentByTaskId(taskId);
            }
        });
    }

    @Override
    public Page<RelatedContent> findAllByTaskIdAndField(String taskId, String field) {
        return managementService.executeCustomSql(new AbstractCustomSqlExecution<RelatedContentEntityMapper, Page<RelatedContent>>(RelatedContentEntityMapper.class) {
            @Override
            public Page<RelatedContent> execute(RelatedContentEntityMapper relatedContentEntityMapper) {
                return relatedContentEntityMapper.findAllByTaskIdAndField(taskId, field);
            }
        });
    }

    @Override
    public Page<RelatedContent> findAllRelatedByProcessInstanceId(String processId) {
        return managementService.executeCustomSql(new AbstractCustomSqlExecution<RelatedContentEntityMapper, Page<RelatedContent>>(RelatedContentEntityMapper.class) {
            @Override
            public Page<RelatedContent> execute(RelatedContentEntityMapper relatedContentEntityMapper) {
                return relatedContentEntityMapper.findAllRelatedByProcessInstanceId(processId);
            }
        });
    }

    @Override
    public Page<RelatedContent> findAllFieldBasedContentByProcessInstanceId(String processId) {
        return managementService.executeCustomSql(new AbstractCustomSqlExecution<RelatedContentEntityMapper, Page<RelatedContent>>(RelatedContentEntityMapper.class) {
            @Override
            public Page<RelatedContent> execute(RelatedContentEntityMapper relatedContentEntityMapper) {
                return relatedContentEntityMapper.findAllFieldBasedContentByProcessInstanceId(processId);
            }
        });
    }

    @Override
    public Page<RelatedContent> findAllContentByProcessInstanceId(String processId) {
        return managementService.executeCustomSql(new AbstractCustomSqlExecution<RelatedContentEntityMapper, Page<RelatedContent>>(RelatedContentEntityMapper.class) {
            @Override
            public Page<RelatedContent> execute(RelatedContentEntityMapper relatedContentEntityMapper) {
                return relatedContentEntityMapper.findAllContentByProcessInstanceId(processId);
            }
        });
    }

    @Override
    public Page<RelatedContent> findAllByProcessInstanceIdAndField(String processId, String field) {
        return managementService.executeCustomSql(new AbstractCustomSqlExecution<RelatedContentEntityMapper, Page<RelatedContent>>(RelatedContentEntityMapper.class) {
            @Override
            public Page<RelatedContent> execute(RelatedContentEntityMapper relatedContentEntityMapper) {
                return relatedContentEntityMapper.findAllByProcessInstanceIdAndField(processId, field);
            }
        });
    }

    @Override
    public int deleteAllContentByProcessInstanceId(String processInstanceId) {
        return managementService.executeCustomSql(new AbstractCustomSqlExecution<RelatedContentEntityMapper, Integer>(RelatedContentEntityMapper.class) {
            @Override
            public Integer execute(RelatedContentEntityMapper relatedContentEntityMapper) {
               return relatedContentEntityMapper.deleteAllContentByProcessInstanceId(processInstanceId);
            }
        });
    }

    @Override
    public Long getTotalContentSizeForUser(String user) {
        return managementService.executeCustomSql(new AbstractCustomSqlExecution<RelatedContentEntityMapper, Long>(RelatedContentEntityMapper.class) {
            @Override
            public Long execute(RelatedContentEntityMapper relatedContentEntityMapper) {
                return relatedContentEntityMapper.getTotalContentSizeForUser(user);
            }
        });
    }
}
